package com.zhu.pre.service.impl;

import com.google.gson.Gson;
import com.sanploy.common.domain.Goods;
import com.sanploy.common.domain.dto.ReduceStockDto;
import com.sanploy.common.domain.vo.GoodsDetailVo;
import com.sanploy.common.domain.vo.Page;
import com.sanploy.common.vo.BaseException;

import com.zhu.pre.mapper.MGoodsMapper;
import com.zhu.pre.service.GoodsAttrService;
import com.zhu.pre.service.MGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhu yingzhi on 2017/9/21.
 * @author yingzhi zhu
 * 手机商品接口逻辑处理实现类
 */
@Service
public class MGoodsServiceImpl implements MGoodsService {

    @Autowired
    private MGoodsMapper mGoodsMapper;

    @Autowired
    private GoodsAttrService goodsAttrService;

    private Gson gson = new Gson();
    @Override
    public Page<Goods> selectAllByPage(int currentPage, int pageSize) {
        Page<Goods> page = new Page<>();
        //查找总数并设置返回的页数，及当前的分页信息
        int total = mGoodsMapper.selectAllByPageCount();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setTotal(total);
        page.setTotalPage((int)Math.ceil((double)total/(double)pageSize));
        page.setList(mGoodsMapper.selectAllByPage((currentPage-1)*pageSize,pageSize));

        return page;
    }

    @Override
    public Page<Goods> selectByCatIdPage(int currentPage, int pageSize, Long catId) {
        Page<Goods> page = new Page<>();
        //查找总数并设置返回的页数，及当前的分页信息
        int total = mGoodsMapper.selectByCatIdPageCount(catId);
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setTotal(total);
        page.setTotalPage((int)Math.ceil((double)total/(double)pageSize));
        page.setList(mGoodsMapper.selectByCatIdPage(catId,(currentPage-1)*pageSize,pageSize));
        return page;
    }

    @Override
    public Page<Goods> selectByHotPage(int currentPage, int pageSize) {
        Page<Goods> page = new Page<>();
        //查找总数并设置返回的页数，及当前的分页信息
        int total = mGoodsMapper.selectByHotPageCount();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setTotal(total);
        page.setTotalPage((int)Math.ceil((double)total/(double)pageSize));
        page.setList(mGoodsMapper.selectByHotPage((currentPage-1)*pageSize,pageSize));
        return page;
    }

    @Override
    public Page<Goods> selectByBestPage(int currentPage, int pageSize) {
        Page<Goods> page = new Page<>();
        //查找总数并设置返回的页数，及当前的分页信息
        int total = mGoodsMapper.selectByBestPageCount();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setTotal(total);
        page.setTotalPage((int)Math.ceil((double)total/(double)pageSize));
        page.setList(mGoodsMapper.selectByBestPage((currentPage-1)*pageSize,pageSize));
        return page;
    }

    @Override
    public Goods selectOne(Long goodsId) {
        return mGoodsMapper.selectOne(goodsId);
    }

    @Override
    public GoodsDetailVo getGoodsById(Long id) throws BaseException {
        GoodsDetailVo goodsDetailVo = mGoodsMapper.getGoodsById(id);
        goodsDetailVo.setGoodsAttr_json(gson.toJson(goodsAttrService.findGoodsAttrByGoodsId(id)));
        return goodsDetailVo;
    }

    @Override
    public void restoreGoodsStock(ReduceStockDto reduceStockDto) {
        for (int i = 0; i < reduceStockDto.getGoodsIds().length; i++) {
            mGoodsMapper.restoreGoodsStock(reduceStockDto.getGoodsNumbers()[i],reduceStockDto.getGoodsIds()[i]);
        }
    }


    @Override
    public void reduceGoodsStock(ReduceStockDto reduceStockDto) {
        for (int i = 0; i < reduceStockDto.getGoodsIds().length; i++) {
            mGoodsMapper.reduceGoodsStock(reduceStockDto.getGoodsNumbers()[i],reduceStockDto.getGoodsIds()[i]);
        }
    }


    @Override
    public List<Goods> getGoodsByIdList(List<Long> idList) throws BaseException {
        return mGoodsMapper.getGoodsByIdList(idList);
    }
}
